import axios from 'axios'
import {ElLoading} from 'element-plus'
import Message from './Message'
import Api from './Api'
const contentTypeForm ='application/x-www-form-urlencoded;charset=UTF-8'
const contentTypeJson ='application/json'
const responseTypeJson = 'json'
let loading = null;
const instance = axios.create({
  withCredentials: true,
  baseURL: (import.meta.env.PROD?Api.prodDomain:"")+"/api",
  timeout: 60*1000
})

// Add at the top of the file, after imports
declare module 'axios' {
  export interface AxiosRequestConfig {
    showLoading?: boolean;
    errorCallback?: (error: any) => void;
    showError?: boolean;
  }
  export interface InternalAxiosRequestConfig {
    showLoading?: boolean;
    errorCallback?: (error: any) => void;
    showError?: boolean;
  }
}

//请求前拦截器
instance.interceptors.request.use(
  (config)=>{
    if (config.showLoading){
        loading=ElLoading.service({
          lock:true,
          text:'加载中....',
          background:'rgba(0,0,0,0.7)',
          customClass: 'custom-loading',
        });
    }
    return config;
  },
  (error)=>{
    if (error.config?.showLoading && loading){
      loading.close();
    }
    Message.error("请求发送失败", 3000);
    return Promise.reject("请求发送失败");
  }
);
//请求后拦截器
instance.interceptors.response.use(
  (response)=>{
    const {showLoading, errorCallback, showError=true, responseType }=response.config;
    if (showLoading && loading){
      loading.close()
    }
    const responseData = response.data;
    if (responseType == "arraybuffer"||responseType == "blob"){
      return responseData;
    }
    //正常请求
    if (responseData.code == 200){
      return responseData;
    }else if (responseData.code == 901){
      //登录超时
      setTimeout(()=>{
        // window.ipcRenderer.send('reLogin')
      },2000);
      return Promise.reject({showError:true,msg:"登录超时"})
    }else{
      //其他错误
      if (errorCallback){
        errorCallback(responseData);
      }
      return Promise.reject({showError:showError,msg:responseData.info })
    }
},
(error)=>{
    if (error.config?.showLoading && loading){
      loading.close();
    }
    Message.error("请求发送失败", 3000);
    return Promise.reject("请求发送失败");
  }
);

interface RequestConfig {
  url: string;
  params: Record<string, any>;
  dataType?: string;
  showLoading?: boolean;
  responseType?: import('axios').ResponseType;
  showError?: boolean;
  errorCallback?: (error: any) => void;
}

const request = (config: RequestConfig) => {
  const { url, params, dataType, showLoading = true, responseType = responseTypeJson, showError = true } = config;
  let contentType = contentTypeForm;
  let formData = new FormData(); // 创建form对象
  for (let key in params) {
    formData.append(key, params[key] == undefined ? "" : params[key]);
  }
  if (dataType != null && dataType === 'json') {
    contentType = contentTypeJson;
  }
  const token = localStorage.getItem('token');
  let headers = {
    'Content-Type': contentType,
    'X-Requested-With': 'XMLHttpRequest',
    "token": token
  };
  return instance.post(url, formData, {
    headers: headers,
    showLoading: showLoading,
    errorCallback: config.errorCallback,
    showError: showError,
    responseType: responseType
  }).catch(error => {
    if (error.showError) {
      Message.error(error.msg, 3000);
    }
    return null;
  });
};

export default request;